package com.ml.carManager.ui.diver.order;

import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.content.ContextCompat;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.hjq.toast.ToastUtils;
import com.jaeger.library.StatusBarUtil;
import com.ml.carManager.R;
import com.ml.carManager.entity.TabEntity;
import com.ml.carManager.net.NetProxyListener;
import com.ml.carManager.ui.dialog.WaitDialog;
import com.ml.carManager.utils.map.AMapUtil;
import com.ml.carManager.utils.map.Location;
import com.ml.carManager.utils.map.LocationUtils;
import com.ml.carManager.widgets.BaseSelectLayout;
import com.ml.carManager.widgets.TouchScrollView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import butterknife.BindView;
import custom.base.entity.base.BaseResponse;
import custom.base.entity.order.OrderDetail;
import custom.base.log.MLog;
import custom.base.ui.activity.BaseActivity;
import retrofit2.Call;

public class OrderDetailActivity extends BaseActivity {
    @BindView(R.id.act_divier_order_detail_no)
    BaseSelectLayout bsNo;
    @BindView(R.id.act_map_mapview)
    MapView mapView;
    @BindView(R.id.act_diver_order_detail_tab)
    CommonTabLayout tabLayout;
    @BindView(R.id.act_diver_order_detail_scrollview)
    TouchScrollView scrollView;


    private AMap aMap;

    private Long orderId;
    private OrderDetail orderDetail = null;

    private ArrayList<CustomTabEntity> tabList = new ArrayList<>();

    private WaitDialog waitDialog;

    @Override
    public boolean initStatusBar() {
        StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), R.color.app_main_color), 0);
        return false;
    }

    @Override
    public int getLayoutID() {
        return R.layout.act_diver_order_detail;
    }

    @Override
    public void initView(@NonNull @NotNull Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        /*
         * 设置离线地图存储目录，在下载离线地图或初始化地图设置;
         * 使用过程中可自行设置, 若自行设置了离线地图存储的路径，
         * 则需要在离线地图下载和使用地图页面都进行路径设置
         * */
        //Demo中为了其他界面可以使用下载的离线地图，使用默认位置存储，屏蔽了自定义设置
        //  MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        if (aMap == null) {
            aMap = mapView.getMap();
        }

        waitDialog = new WaitDialog(getActivity());
        scrollView.setTopHeight(500);
        scrollView.setView(mapView);
    }

    @Override
    public void initData(@NonNull @NotNull Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        orderId = getIntent().getExtras().getLong("orderId", 0);
        if (orderId == null || orderId == 0) {
            ToastUtils.show("订单ID为空");
            return;
        }
        getOrderDetail();

        tabList.add(new TabEntity("任务信息", R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        tabList.add(new TabEntity("运输信息", R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        tabLayout.setTabData(tabList);
    }

    @Override
    public void initListener() {
        super.initListener();
//        btnExitLogin.setOnClickListener(this);

        LocationUtils.getInstance(getApplicationContext()).addLocationListener(new LocationUtils.OnLocationListener() {
            @Override
            public void onLocationFinish(Location location) {
                MLog.test(location);
                AMapUtil.animateCamera(aMap, location.getLat(), location.getLng(), 15, 1000);
            }
        });
    }


    @Override
    public void onClick(View v, int clickID) {
//        if (clickID == btnExitLogin.getId()) {
//            LocationUtils.getInstance(getApplicationContext()).requestLocated(getActivity());
//
//        }
    }

    @Override
    protected void onDestroy() {
        LocationUtils.getInstance(getApplicationContext()).stopLocation();
//        LocationUtils.getInstance(getApplicationContext()).removeLocation(this);
        super.onDestroy();
    }

    private void getOrderDetail() {
        Call<BaseResponse<OrderDetail>> call = appApi.getOrderDetail(orderId);
        call.enqueue(new NetProxyListener<OrderDetail>(getALiveNetCallback()) {
            @Override
            protected boolean dealNullResponseData() {
                return true;
            }

            @Override
            public void onResponseSuccess(Call call, BaseResponse<OrderDetail> response) {
                orderDetail = response.getData();

                setData();
            }

            @Override
            public void onResponseError(Call call, Throwable t) {
                super.onResponseError(call, t);
            }

            @Override
            public void onResponseCodeError(Call call, BaseResponse<OrderDetail> response) {
                super.onResponseCodeError(call, response);
            }
        });
    }

    private void setData() {
        if (orderDetail == null) {
            return;
        }
        bsNo.setContent(orderDetail.getNo());

    }


}
